package com.itheima.controller;

import com.itheima.pojo.Address;
import com.itheima.pojo.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/*
    测试响应数据
    @ResponseBody注解用于响应数据 可以将方法返回值直接响应，
    如果返回值类型是实体对象、集合，将会转化成JSON类型的数据返回

    @RestController注解中包含了@Controller注解和@ResponseBody注解
*/
@RestController
public class ResponseController {

    /*@RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }

    @RequestMapping("/getAddress")
    public Address getAddress(){
        Address addr = new Address();
        addr.setProvince("广东");
        addr.setCity("深圳");
        return addr;
    }

    @RequestMapping("listAddress")
    public List<Address> listAddress(){
        List<Address> list = new ArrayList<>();

        Address addr1 = new Address();
        addr1.setProvince("广东");
        addr1.setCity("深圳");

        Address addr2 = new Address();
        addr2.setProvince("江西");
        addr2.setCity("南昌");

        list.add(addr1);
        list.add(addr2);
        return list;
    }*/

    // 使用上述方法返回的数据格式不同，不利于前端程序员的理解
    // 因此就需要有一个统一的写法
    // 定义一个Result类用于统一存储响应的数据

    @RequestMapping("/hello")
    public Result hello(){
        //return new Result(1, "success", "Hello world");
        return Result.success("Hello World");  // Result类中定义了success方法用于简化写法
    }

    @RequestMapping("/getAddress")
    public Result getAddress(){
        Address addr = new Address();
        addr.setProvince("广东");
        addr.setCity("深圳");
        return Result.success(addr);
    }

    @RequestMapping("listAddress")
    public Result listAddress(){
        List<Address> list = new ArrayList<>();

        Address addr1 = new Address();
        addr1.setProvince("广东");
        addr1.setCity("深圳");

        Address addr2 = new Address();
        addr2.setProvince("江西");
        addr2.setCity("南昌");

        list.add(addr1);
        list.add(addr2);
        return Result.success(list);
    }
}
